package com.wangyg.juc.join.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wangyg6 on 2018/7/24.
 */
public class Test {
	public static void main(String[] args) throws InterruptedException {
		Lock lock = new ReentrantLock();
		Condition t2 = lock.newCondition();
		Condition t3 = lock.newCondition();
		Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				lock.lock();
				System.out.println("T1 run");
				t2.signal();
				lock.unlock();
			}
		});
		Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				lock.lock();
				try {
					System.out.println("t2.wait");
					t2.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("T2 run");
				t3.signal();
				lock.unlock();
			}
		});

		Thread thread3 = new Thread(new Runnable() {
			@Override
			public void run() {
				lock.lock();
				try {
					System.out.println("t3.wait");
					t3.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("T3 run");
				lock.unlock();
			}
		});

		thread3.start();
		Thread.sleep(1000);

		thread2.start();
		Thread.sleep(100);

		thread1.start();
	}
}
